package it.aekidna.cleverplatypus.workflow.template
{
	import flash.events.Event;

	import it.aekidna.cleverplatypus.gui.containers.commonpopup.GetStringValuePopUpDialog;
	import it.aekidna.cleverplatypus.mediator.CommandEvent;
	import it.aekidna.cleverplatypus.workflow.AbstractWorkflowTask;

	import mx.rpc.events.ResultEvent;

	public class AskStringTask extends AbstractWorkflowTask
	{
		public var prompt : String = "Filename";
		public var dialog : GetStringValuePopUpDialog;

		[Bindable]
		public var model : Object;
		[Bindable]
		public var hotSpotName : String;
		public var resultField : String = null;
		public var closeOnSubmit : Boolean = true;


		public function AskStringTask()
		{
			super();
		}


		override public function begin() : void
		{
			dialog = new GetStringValuePopUpDialog();
			dialog.prompt = prompt;
			dialog.closeOnSubmit = this.closeOnSubmit;
			dialog.hotSpotName = hotSpotName;
			dialog.stringValue = model[ resultField ];
			dialog.addEventListener( ResultEvent.RESULT, onNameSet );
			dialog.addEventListener( Event.CANCEL, onCancel );
			CommandEvent.createWithNamedParams( "SHOW_CONTEXTUAL_POP_UP_PANEL", { panel: dialog }).dispatch();
		}


		protected function onNameSet( inEvent : ResultEvent ) : void
		{
			model[ resultField ] = inEvent.result as String;
			complete();
		}


		protected function onCancel( inEvent : Event ) : void
		{
			fail( "User aborted operation" );
		}



	}
}